package com.treasure.mall.zbqmallapi.controller.integral;

import com.treasure.mall.biz.dto.condition.CommonCondition;
import com.treasure.mall.biz.entity.integral.IntegralProductPO;
import com.treasure.mall.biz.service.core.integral.IntegralProductService;
import com.treasure.mall.biz.vo.PageDataVO;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbqmallapi.vo.integral.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/8/25
 */
@RestController
@RequestMapping("/integral/product")
public class IntegralProductController {
    @Autowired
    private IntegralProductService productService;

    /**
     * 积分商品列表
     *
     * @param condition
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ApiResult list(
            CommonCondition condition,
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "20") int size
    ) {
        Page<IntegralProductPO> pages = productService.findAll(page, size, condition);
        PageDataVO<ProductVO> pageDataVO = new PageDataVO<>();
        pageDataVO.setPageCount(pages.getTotalPages());
        pageDataVO.setPageIndex(page);
        pageDataVO.setPageSize(size);
        pageDataVO.setTotal(pages.getTotalElements());

        List<ProductVO> vos = pages.getContent().stream().map(ProductVO::new).collect(Collectors.toList());
        pageDataVO.setRows(vos);

        return ApiResult.ok(pageDataVO);
    }

    /**
     * 积分商品详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public ApiResult detail(@PathVariable Long id) {
        IntegralProductPO po = productService.findById(id);

        return ApiResult.ok(new ProductVO(po));
    }


}
